package net.projecteuler.problem4;

public class Palindrome {
	
	public int getLargestPalindrome(int digit) {
		int maxResult = 0;
		int ab = 0;
		
		int digitNumber = getDigitNumber(digit);

		for(int a = digitNumber; a >= (digitNumber + 1) / 10; a--) {
			for(int b = digitNumber; b >= (digitNumber + 1) / 10; b--) {
				ab = a * b;
				
				if(isPalindrome(String.valueOf(ab))) {
					maxResult = ab > maxResult ? ab : maxResult;
				}
				
			}
		}

		return maxResult;
	}
	
	private String reverse(String str) {
		return new StringBuffer(str).reverse().toString();
	}
	
	public int getDigitNumber(int digit) {
		int coefficient = 10;
		int digitNumber = 1;

		for(int i = 1; i <= digit; i++) {
			digitNumber *= 10;
		}
		digitNumber -= 1;
		return digitNumber;
	}
	
	public boolean isPalindrome(String ab) {
		String ba = reverse(ab);
		if(ab.equals(ba)) {
			return true;
		}
		return false;
	}
	
}
